from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import status
# Create your views here.
from reptile import land_sise_get_info
from .models import Userinfo,classinfo
from Class.serializers import classinfo_serializers


@api_view(['GET','POST'])
def Class_info_view(request):
    if request.method =='GET':
        user = request.GET['user']
        s = classinfo_serializers(classinfo.objects.filter(user=user),many=True)
        return Response(s.data,status=status.HTTP_200_OK)


@api_view(['GET','POST'])
def Users_info_view(request):
    if request.method == 'POST':
        data = request.data
        username = data['username']
        password = data['password']
        a = land_sise_get_info(url='http://class.seig.edu.cn:7001/sise/module/student_schedular/student_schedular.jsp',
                               username=username,
                               password=password)
        if a:
            for i in range(56):
                week = a[i][0]
                time = a[i][1]
                clas = a[i][2]
                classinfo.objects.create(user=username,week=week,time=time,class_info=clas)
            Userinfo.objects.create(user_type=1,username=username,password=password)
            return Response(status=status.HTTP_200_OK)
    return Response(status=status.HTTP_400_BAD_REQUEST)
# {
# "username":"2040231145",
# "password":"20440883106016@SEIG!sise"
# }